package com.sskj.bibi.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.mvc.MVCHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.bibi.R;
import com.sskj.bibi.R2;
import com.sskj.bibi.bean.EntrustBean;
import com.sskj.bibi.presenter.AllEntrustActivityPresenter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.TipUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.BIBI_All_ENTRUST)
public class AllEntrustActivity extends BaseActivity<AllEntrustActivityPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;

    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter slimAdapter;
    private int curPos;
    private MaterialDialog cancelTip;
    private MVCHelper<List<EntrustBean>> mvcHelper;
    @Autowired
    String qu;

    @Override
    protected int getLayoutId() {
        return R.layout.bibi_activity_all_entrust;
    }

    @Override
    public AllEntrustActivityPresenter getPresenter() {
        return new AllEntrustActivityPresenter();
    }

    @Override
    protected void initView() {

        ARouter.getInstance().inject(this);
        setTitle(App.INSTANCE.getString(R.string.bibi_allEntrustActivity1));
        tvTitle.setTextColor(getResources().getColor(R.color.commonWhite));
        setRightTitle(App.INSTANCE.getString(R.string.bibi_allEntrustActivity2), (v) -> {
            ARouter.getInstance().build(RConfig.BIBI_HISTORY_ENTRUST).withString(Constans.QU, qu).navigation();
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setLastDraw(false)
                .setLeftPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 29))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 2))
                .setPaintColor(ContextCompat.getColor(App.INSTANCE, R.color.bibi_divider))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.bibi_recy_item_current_entrust, new SlimInjector<EntrustBean>() {
            @Override
            public void onInject(EntrustBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tvOrderType, data.getType().equals("1") ? App.INSTANCE.getString(R.string.bibi_historyEntrustActivity5) : App.INSTANCE.getString(R.string.bibi_historyEntrustActivity6))
                        .textColor(R.id.tvOrderType, ContextCompat.getColor(App.INSTANCE, data.getType().equals("1") ? R.color.bibi_03c : R.color.bibiRedText))
                        .text(R.id.tvCoinType, data.getL_code() + "/" + data.getR_code())
                        .text(R.id.tvTime, data.getAdd_time())
                        .text(R.id.tvDeal, App.INSTANCE.getString(R.string.bibi_allEntrustActivity5))
                        .text(R.id.tvTitlePrice, String.format(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity8), data.getR_code()))
                        .text(R.id.tvTitleNum, data.getOtype().equals("2") && data.getType().equals("1") ? App.INSTANCE.getString(R.string.bibi_allEntrustActivity7) : String.format(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity13), data.getL_code()))
                        .text(R.id.tvTitleDealNum, String.format(App.INSTANCE.getString(R.string.bibi_allEntrustActivity9), data.getL_code()))
                        .text(R.id.tvPrice, data.getOtype().equals("2") ? App.INSTANCE.getString(R.string.bibi_historyEntrustActivity7) : data.getWtprice())
                        .text(R.id.tvNum, data.getOtype().equals("2") && data.getType().equals("1") ? data.getTotalprice() : data.getWtnum())
                        .text(R.id.tvDealNum, data.getCjnum())
                        .clicked(R.id.tvDeal, (v) -> {
                            curPos = slimAdapter.getData().indexOf(data);
                            dealCancel();
                        });
            }
        }).attachTo(recyclerView);
        slimAdapter.updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        ModelRx2DataSource<EntrustBean> refreshDataSource = new ModelRx2DataSource<EntrustBean>((ModelRx2DataSource.OnLoadSource<EntrustBean>) page -> {
            if (TextUtils.isEmpty(SPUtil.get(SPConfig.TOKEN, ""))) {
                return Flowable.just(new ArrayList<EntrustBean>());
            } else {
                return mPresenter.getEntrustList(page, qu);
            }
        }, 10);
        // 设置数据源
        mvcHelper.setDataSource(refreshDataSource);
        // 设置适配器
        mvcHelper.setAdapter(slimAdapter);
    }


    private void dealCancel() {
        if (cancelTip == null) {
            cancelTip = TipUtil.getSureCancelTip(this, App.INSTANCE.getString(R.string.bibi_allEntrustActivity11), () -> {
                mPresenter.cancelEntrust(((EntrustBean) slimAdapter.getDataItem(curPos)).getOrders_id() + "");
            });
        }
        cancelTip.show();
    }

    @Override
    public void initData() {
        mvcHelper.refresh();
    }

    public void updateUI(List<EntrustBean> data) {
        if (coolRefreshView.isRefreshing()) {
            coolRefreshView.setRefreshing(false);
        }
        slimAdapter.updateData(data);
    }

    public void cancelReturn() {
        mvcHelper.refresh();

    }

}
